﻿using System;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for TAAgeEligibilityDB
/// </summary>
/// 
namespace CE.TravelInsurance
{
    public class TAAgeEligibilityDB
    {
        public TAAgeEligibilityDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(TAAgeEligibility taageeligibility)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocTAAgeEligibilityInsertUpdateSingleItem";

            SortedList _sPara = new SortedList();
            #region Parameter

            _sPara.Add("@TAAgeId", taageeligibility.TAAgeId);
            _sPara.Add("@TAInsID", taageeligibility.TAInsId);
            _sPara.Add("@AdultAgeFrom", taageeligibility.AdultAgeFrom);
            _sPara.Add("@AdultAgeTo", taageeligibility.AdultAgeTo);
            _sPara.Add("@ChildAgeFrom", taageeligibility.ChildAgeFrom);
            _sPara.Add("@ChildAgeTo", taageeligibility.ChildAgeTo);
            _sPara.Add("@PlanType", taageeligibility.PlanType);


            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public bool Delete(Int64 TAAgeId)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocTAAgeEligibilityDeleteSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@TAAgeID", TAAgeId);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result > 0;
        }


        public TAAgeEligibility GetAgeEligibilityDTItem(Int64 TAInsId, string PlanType)
        {
            TAAgeEligibility _taageeligibility = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocTAAgeEligibilitySelectSingleItem";

            SortedList _sPara = new SortedList();
            _sPara.Add("@TAInsID", TAInsId);
            _sPara.Add("@PlanType", PlanType);
            
            using (DataTable _dt = _db.GetDataTableBySP(_spName, _sPara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _taageeligibility = new TAAgeEligibility();
                    _taageeligibility = FillDataRecord(_dt, 0);
                }
            }
            return _taageeligibility;
        }

        public TAAgeEligibility GetAgeEligibilityDTItem(Int64 TaCompanyId, string PlanType, string CoverageLevel)
        {
            TAAgeEligibility _taageeligibility = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocTAAgeEligibilitySelectSingleItemSelected";

            SortedList _sPara = new SortedList();
            _sPara.Add("@TaCompanyId", TaCompanyId);
            _sPara.Add("@PlanType", PlanType);
            _sPara.Add("@CoverageLevel", CoverageLevel);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _sPara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _taageeligibility = new TAAgeEligibility();
                    _taageeligibility = FillDataRecord(_dt, 0);
                }
            }
            return _taageeligibility;
        }

        private TAAgeEligibility FillDataRecord(DataTable myDataTable, int rowId)
        {
            TAAgeEligibility myTAAgeEligibility = new TAAgeEligibility();
            myTAAgeEligibility.TAAgeId = Convert.ToInt64(myDataTable.Rows[rowId]["TAAgeID"].ToString());
            myTAAgeEligibility.TAInsId = Convert.ToInt64(myDataTable.Rows[rowId]["TAInsID"].ToString());
            myTAAgeEligibility.AdultAgeFrom = Convert.ToInt32(myDataTable.Rows[rowId]["AdultAgeFrom"].ToString());
            myTAAgeEligibility.AdultAgeTo = Convert.ToInt32(myDataTable.Rows[rowId]["AdultAgeTo"].ToString());
            myTAAgeEligibility.ChildAgeFrom = Convert.ToInt32(myDataTable.Rows[rowId]["ChildAgeFrom"].ToString());
            myTAAgeEligibility.ChildAgeTo = Convert.ToInt32(myDataTable.Rows[rowId]["ChildAgeTo"].ToString());
            myTAAgeEligibility.PlanType = myDataTable.Rows[rowId]["PlanType"].ToString();
            myTAAgeEligibility.PlanName = myDataTable.Rows[rowId]["PlanName"].ToString();
            myTAAgeEligibility.CoverageLevel = myDataTable.Rows[rowId]["CoverageLevel"].ToString();

            return myTAAgeEligibility;
        }
    }
}
